<?php
/**
 * +----------------------------------------------------------------------
 * | Copyright © Sogu Technologies Co. Ltd 2018-2050 All rights reserved.
 * +----------------------------------------------------------------------
 * | File name: OcrIssueCode.php
 * +----------------------------------------------------------------------
 * | Author: sam
 * +----------------------------------------------------------------------
 * | Description: 获取用户身份证签发机关代码类
 * +----------------------------------------------------------------------
 * | Others: 其他内容的说明
 * +----------------------------------------------------------------------
 * | History: 修改历史记录列表，每条修改记录应包含修改日期、修改者及修改内容简述
 * +----------------------------------------------------------------------
 * | 1.Date: 2018/12/28 Author: sam Modification: 创建
 * +----------------------------------------------------------------------
 */

namespace App\Library\Guzzle;


class OcrIssueCode
{
    /**
     * Description:根据签发机关字符串获取签发机关代码
     * Author: Sam
     * DateTime: 2018/12/28 15:06
     * @param $data
     * @return mixed
     */
    public static function getCode($data)
    {
        $all_code = config('issue_gov_code');
        $district_arr = [];
        foreach ($all_code as $index =>$item)
        {
            $district = str_replace(['县','区','市'],['','',''],$item['district']);
            if(empty($district)){
                continue;
            }
            if(strpos($data,$district)!==false){
                $district_arr[] = $item;
            }
        }
        if(count($district_arr)>1){
            foreach ($district_arr as $key => $value){
               similar_text($data,$value['province'].$value['city'].$value['district'],$percent);
               $district_arr[$key]['similar'] = round($percent,2);
            }
            $similar = array_column($district_arr, 'similar');
            array_multisort($similar,SORT_DESC,$district_arr);
        }
        $res = !empty($district_arr[0])?$district_arr[0]['code']:0;
        return $res;
    }
}